package org.streets.workflow.engine.task;

import java.util.List;

import org.streets.workflow.engine.ITaskInstance;
import org.streets.workflow.engine.IWorkItem;
import org.streets.workflow.engine.WorkflowEngine;
import org.streets.workflow.engine.WorkflowException;
import org.streets.workflow.model.WFFormTask;


/**
 * 动态任务分配器，用于指定后续环节的操作员。
 */
public class DefaultDynamicTaskAssignor implements ITaskAssignor{
	/**
	 * 工作项是否需要签收
	 */
    boolean needClaim = false;
    
    /**
     * 操作员Id列表
     */
    List<String> actorIdsList = null;
   
    public List<String> getActorIdsList() {
        return actorIdsList;
    }

    public void setActorIdsList(List<String> actorIdsList) {
        this.actorIdsList = actorIdsList;
    }

    public boolean isNeedClaim() {
        return needClaim;
    }

    public void setNeedClaim(boolean needSign) {
        this.needClaim = needSign;
    }
    
    public void assign(WorkflowEngine engine, ITaskInstance task, String performerName)
            throws WorkflowException {

        if (actorIdsList==null || actorIdsList.size()==0){
            throw new WorkflowException(task.getProcess().getId(),task.getProcess().getModel(),
                    task.getTaskModelId(),"actorIdsList can not be empty");
        }

        List<IWorkItem> workItems = engine.assignToActors(task, actorIdsList);
        
        //如果不需要签收，这里自动进行签收，（FormTask的strategy="all"或者=any并且工作项数量为1）
        if (!needClaim){
            if (WFFormTask.ALL.equals(task.getAssignStrategy()) ||
                    (WFFormTask.ANY.equals(task.getAssignStrategy()) && actorIdsList.size()==1)){
                for (int i=0;i<workItems.size();i++){
                    IWorkItem wi = workItems.get(i);
                    engine.claimWorkItem(wi);
                }
            }
        }
    }
}
